<template>
  <a-layout>
    <a-layout-content
        :style="{ background: '#fff', padding: '24px', margin: 0, minHeight: '280px' }"
    >
      <a-space size="small">
      <a-input-search
            v-model:value="KeyValue"
            placeholder="输入名称"
            enter-button="查询"
            size="large"
            @search="handleQuery({page: 1,size: pagination.pageSize, loginName:KeyValue})"
            style="width: 400px"
        />
        <a-button type="primary" @click="add()" size="large">
          新增
        </a-button>
      </a-space>
      <a-table :columns="columns"
               :row-key="record => record.id"
               :data-source="users"
               :pagination="pagination"
               :loading="modalLoading"
               @change="handleTableChange"
      >
        <template v-slot:action="{ text, record }">
          <a-space size="small">
            <a-button type="primary" @click="resetPassword(record)">
              重置密码
            </a-button>
            <a-button type="primary" @click="edit(record)">
              编辑
            </a-button>
            <a-popconfirm
                title="删除后不可恢复，确认删除?"
                ok-text="是"
                cancel-text="否"
                @confirm="handleDelete(record.id)"
            >
              <a-button type="primary" danger>
                删除
              </a-button>
            </a-popconfirm>
          </a-space>
        </template>
      </a-table>
    </a-layout-content>
  </a-layout>

  <a-modal title="用户表单"
           v-model:visible="modalVisible"
           @ok="handleModalOk"
           :confirm-loading="modalLoading">
    <a-form :model="user" :label-col="{ span: 6}">
      <a-form-item label="登录名">
        <a-input v-model:value="user.loginName" :disabled="!!user.id"/>
      </a-form-item>
      <a-form-item label="昵称">
        <a-input v-model:value="user.name" />
      </a-form-item>
      <a-form-item label="密码" v-show="!user.id">
        <a-input v-model:value="user.password"/>
      </a-form-item>
    </a-form>
  </a-modal>

  <a-modal title="重置密码"
           v-model:visible="resetModalVisible"
           @ok="handleResetModalOk"
           :confirm-loading="resetModalLoading">
    <a-form :model="user" :label-col="{ span: 6}">
      <a-form-item label="新密码">
        <a-input v-model:value="user.password"/>
      </a-form-item>
    </a-form>
  </a-modal>

</template>

<script lang="ts">
import {defineComponent, onMounted, ref} from 'vue';
import axios from 'axios'
import {message} from "ant-design-vue";
import {Tool} from "@/util/tool";

declare let hexMd5: any;
declare let KEY: any;

export default defineComponent({
  name: 'AdminUser',
  setup() {
    console.log("setup");
    const users = ref();
    const pagination = ref({
      current: 1,
      pageSize: 10,
      total: 0
    });
    const loading = ref(false);
    const KeyValue = ref<string>('');
    const columns = [
      {
        title: '登录名',
        dataIndex: 'loginName',
      },
      {
        title: '名称',
        dataIndex: 'name',
      },
      {
        title: '密码',
        dataIndex: 'password',
      },
      {
        title: 'action',
        key: 'action',
        slots: {customRender: 'action'}
      }
    ];

    /**
     * 数据查询
     **/
    const handleQuery = (params: any) => {
      loading.value = true;
      //axios.get("/user/list", params)会造成params分离成单个的param，导致后端无法正确获得参数
      axios.get("/user/list", {
          params: {
            page: params.page,
            size: params.size,
            loginName: params.loginName,
          }
          }).then((response) => {
        loading.value = false;
        const data = response.data;
        if (data.success) {
          users.value = data.content.list;

          //重置分页按钮
          pagination.value.current = params.page;
          pagination.value.total = data.content.total;
        }else {
          message.error(data.message);
        }
      })
    };

    /**
     * 表格点击页码时触发
     **/
    const handleTableChange = (pagination: any) => {
      console.log("" + pagination);
      handleQuery({
        page: pagination.current,
        size: pagination.pageSize
      });
    };

    //---------------表单-----------------

    const user = ref();
    const modalVisible = ref(false);
    const modalLoading = ref(false);
    const handleModalOk = (params: any) => {
      modalLoading.value = true;

      user.value.password = hexMd5(user.value.password + KEY);

      axios.post("/user/save",user.value).then((response) => {
        modalLoading.value = false;
        const data = response.data; // data == commonResp
        if (data.success) {
          modalVisible.value = false;
          modalLoading.value = false;

          //重新加载列表
          handleQuery({
            page: pagination.value.current,
            size: pagination.value.pageSize
          });
        }else {
          message.error(data.message);
        }
      });
    };

    /**
    *编辑
    **/
    const edit = (record: any) =>{
      modalVisible.value = true;
      //不复制会指向同一内存地址,导致数值绑定
      user.value = Tool.copy(record);
    };
    /**
     *新增
     **/
    const add = () =>{
      modalVisible.value = true;
      user.value = {};
    };
    /**
     *删除
     **/
    const handleDelete = (id: number) =>{
      axios.delete("/user/delete/" + id).then((response) => {

        const data = response.data; // data == commonResp
        if (data.success) {
          //重新加载列表
          handleQuery({
            page: pagination.value.current,
            size: pagination.value.pageSize
          });
        }
      });
    };

    //---------------重置密码-----------------
    const resetModalVisible = ref(false);
    const resetModalLoading = ref(false);
    const handleResetModalOk = (params: any) => {
      resetModalLoading.value = true;

      user.value.password = hexMd5(user.value.password + KEY);

      axios.post("/user/reset-password",user.value).then((response) => {
        resetModalLoading.value = false;
        const data = response.data; // data == commonResp
        if (data.success) {
          resetModalVisible.value = false;
          //重新加载列表
          handleQuery({
            page: pagination.value.current,
            size: pagination.value.pageSize
          });

          resetModalLoading.value = false;
        }else {
          message.error(data.message);
        }
      });
    };

    /**
     *重置密码
     **/
    const resetPassword = (record: any) =>{
      resetModalVisible.value = true;
      //不复制会指向同一内存地址,导致数值绑定
      user.value = Tool.copy(record);
      user.value.password = null;
    };

    onMounted(() => {
      handleQuery({
        page: 1,
        size: pagination.value.pageSize
      });
    });

    return {
      columns,
      users,
      loading,
      pagination,
      handleTableChange,

      modalVisible,
      modalLoading,
      handleModalOk,
      handleQuery,
      user,
      KeyValue,

      add,
      edit,
      handleDelete,

      resetModalVisible,
      resetModalLoading,
      handleResetModalOk,
      resetPassword,
    }
  }
});
</script>